<!-- 成绩标准管理-->
<template>
  <div class="courseBox">
    <div class="courseBoxTop">
      <div class="courseBoxTopSearch">
        <div class="standards">
          <span>标准名称：</span>
          <el-input
            v-model="formSearch.gradeStandardName"
            placeholder="请输入标准名称"
          ></el-input>
        </div>

        <div class="standards">
          <span style="width: 160px">考试类型：</span>
          <el-select v-model="formSearch.typeId" placeholder="请选择考试类型">
            <el-option
              v-for="item in option"
              :key="item.id"
              :label="item.typeName"
              :value="item.typeId"
            ></el-option>
          </el-select>
        </div>

        <div class="standards">
          <span>状态：</span>
          <el-select v-model="formSearch.status" placeholder="请选择状态">
            <el-option label="启用" value="0"></el-option>
            <el-option label="停用" value="1"></el-option>
          </el-select>
        </div>

        <div class="pictureVersion">
          <span>时间阶段：</span>
          <div class="block">
            <el-date-picker
              v-model="formSearch.params.beginTime"
              align="right"
              type="date"
              placeholder="开始时间"
            >
            </el-date-picker>
            <span>-</span>
            <el-date-picker
              v-model="formSearch.params.endTime"
              align="right"
              type="date"
              placeholder="结束时间"
            >
            </el-date-picker>
          </div>
        </div>

        <div class="searchAndReset">
          <el-button class="searchBtn" @click="search">
            <i class="el-icon-search"></i>
            <span>搜索</span>
          </el-button>
          <el-button class="resetBtn" @click="refreshInput">
            <i class="el-icon-refresh"></i>
            <span>重置</span>
          </el-button>
        </div>
      </div>
    </div>

    <div class="courseBoxBottom">
      <div class="bottomHeader">
        <span class="title">管理列表</span>
        <div class="bottomHeaderBtn">
          <el-button class="addBtn" @click="openAddDialog">
            <i class="el-icon-plus"></i>
            <span>新增</span>
          </el-button>
          <el-button class="delBtn" @click="batchDelStandard">
            <i class="el-icon-close"></i>
            <span>批量删除</span>
          </el-button>
        </div>
      </div>

      <div class="bottomMian">
        <!-- 表格 -->
        <el-table
          ref="multipleTable"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column type="index" label="序号" width="55">
          </el-table-column>
          <el-table-column
            label="标准名称"
            prop="gradeStandardName"
          ></el-table-column>
          <el-table-column prop="typeName" label="考试类型"> </el-table-column>
          <el-table-column prop="status" label="状态">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-color="#13ce66"
                inactive-color="#C2C2C2"
                @change="updateIsUsing(scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" sortable>
          </el-table-column>
          <el-table-column fixed="right" label="操作">
            <template slot-scope="scope">
              <span
                @click="openEditDialog(scope.row)"
                class="icon edit"
              >编辑</span>
              <span
                @click="delStandard(scope.row)"
                class="icon del"
              >删除</span>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination" v-if="tableData.length > 0">
          <el-pagination
            @current-change="handleCurrentChange"
            :current-page="queryParams.pageNum"
            :page-size="10"
            prev-text="上一页"
            next-text="下一页"
            layout=" prev, pager, next"
            :total="total"
          >
          </el-pagination>
          <div class="jumper">
            跳到第
            <input type="text" ref="currentPage" value="1" />
            页
          </div>
          <button class="submit" @click="goPage">确定</button>
        </div>
      </div>
    </div>

    <!-- 新增-->
    <div class="dialog">
      <el-dialog title="添加" :visible.sync="addDialog" center>
        <span>
          <el-form ref="form" :model="form" label-width="80px">
            <el-form-item label="标准名称">
              <el-input
                v-model="form.gradeStandardName"
                placeholder="请输入标准名称"
              ></el-input>
            </el-form-item>
            <el-form-item label="考试类型">
              <el-select v-model="form.typeId" placeholder="请选择考试类型">
                <el-option
                  v-for="item in option"
                  :key="item.typeId"
                  :label="item.typeName"
                  :value="item.typeId"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="评分等级">
              <el-table
                :data="form.scoreGrade"
                height="230"
                border
                style="width: 100%"
              >
                <el-table-column label="等级">
                  <template slot-scope="scope">
                    <el-input type="text" v-model="scope.row.grade"></el-input>
                  </template>
                </el-table-column>
                <el-table-column label="标准分数">
                  <template slot-scope="scope">
                    <el-input type="text" v-model="scope.row.score"></el-input>
                  </template>
                </el-table-column>
              </el-table>
              <el-button
                icon="el-icon-plus"
                circle
                @click="tianjia"
                class="btn"
              ></el-button>
            </el-form-item>
          </el-form>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button @click="closeAddDialog">取 消</el-button>
          <el-button type="primary" @click="addSubmit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import "@/styles/picture.scss";
import { _debounce } from "@/utils/debounce";
import moment from "moment";
import { getGradeListApi } from "@/api/basics/teachingPlan";
import {
  getStandardListApi,
  addStandardListApi,
  delStandardListApi,
  editStandardListApi,
} from "@/api/achievement/standards";

export default {
  data() {
    return {
      //搜索框数据
      formSearch: {
        gradeStandardName: "",
        status: "",
        typeId: "",
        params: { beginTime: "", endTime: "" },
      },
      option: [],
      tableData: [], //表格
      //分页
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0, //总条数
      addDialog: false, //添加弹出框
      editDialog: false, //修改弹框
      isEdit: false,
      //添加
      form: {
        gradeStandardId: "",
        gradeStandardName: "",
        scoreGrade: [],
        typeId: "",
      },
      multipleSelection: [],
    };
  },

  mounted() {
    // 获取成绩标准列表
    this.getStandardList({
      ...this.queryParams,
      params: { beginTime: "", endTime: "" },
    });

    // 获取 考试类型 列表
    this.getGradeList();
  },

  methods: {
    // 获取成绩标准列表
    getStandardList(data) {
      getStandardListApi({
        ...data,
      }).then((res) => {
        if (res.code === 200) {
          this.total = res.total;
          this.tableData = res.rows.map((item) => {
            return {
              ...item,
              scoreGrade: JSON.parse(item.scoreGrade),
              status: item.status == "0" ? true : false,
            };
          });
        }
      });
    },

    // 获取 考试类型 列表
    async getGradeList() {
      let res = await getGradeListApi({ parentId: 147 });
      this.option = res.rows;
    },

    //搜索
    search() {
      let time = {
        beginTime: this.formSearch.params.beginTime
          ? moment(this.formSearch.params.beginTime).format(
              "YYYY-MM-DD 00-00-00"
            )
          : "",
        endTime: this.formSearch.params.endTime
          ? moment(this.formSearch.params.endTime).format("YYYY-MM-DD 23-59-59")
          : "",
      };
      let data = {
        ...this.formSearch,
        ...time,
      };
      this.getStandardList({ ...this.queryParams, ...data });
    },

    // 重置
    refreshInput: _debounce(function () {
      this.formSearch = {
        gradeStandardName: "",
        status: "",
        typeId: "",
        params: { beginTime: "", endTime: "" },
      };

      this.getStandardList({
        ...this.queryParams,
        params: { beginTime: "", endTime: "" },
      });
    }, 200),

    // 点击新增
    openAddDialog() {
      this.form = {
        gradeStandardId: "",
        gradeStandardName: "",
        scoreGrade: [],
        typeId: "",
      };
      this.isEdit = false;
      this.addDialog = true;
      this.form.scoreGrade = [];
    },

    // 取消新增
    closeAddDialog() {
      this.addDialog = false;
    },

    // 点击编辑
    openEditDialog(data) {
      this.form = {
        gradeStandardId: data.gradeStandardId,
        gradeStandardName: data.gradeStandardName,
        scoreGrade: data.scoreGrade ? [...data.scoreGrade] : [],
        typeId: data.typeId,
      };
      this.isEdit = true;
      this.addDialog = true;
    },

    //新增添加表格数据
    tianjia() {
      this.form.scoreGrade.push({ grade: "", score: "" });
    },

    //添加
    addSubmit() {
      if (this.isEdit) {
        editStandardListApi({ ...this.form }).then((res) => {
          if (res.code === 200) {
            this.addDialog = false;
            // 获取成绩标准列表
            this.getStandardList({
              ...this.queryParams,
              params: { beginTime: "", endTime: "" },
            });
            this.$message.success("修改成功!");
          } else {
            this.$message.error("修改失败!");
          }
        });
      } else {
        addStandardListApi({ ...this.form, status: "0" }).then((res) => {
          if (res.code === 200) {
            this.addDialog = false;
            // 获取成绩标准列表
            this.getStandardList({
              ...this.queryParams,
              params: { beginTime: "", endTime: "" },
            });
            this.$message.success("添加成功!");
          } else {
            this.$message.error("添加失败!");
          }
        });
      }
    },

    // 删除
    delStandard(data) {
      let ids = data.gradeStandardId + "";
      this.$confirm("此操作将删除已选择的成绩标准, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delStandardListApi({ ids }).then((res) => {
            console.log(res);
            if (res.code === 200) {
              this.$message.success("删除成功!");
              // 获取成绩标准列表
              this.getStandardList({
                ...this.queryParams,
                params: { beginTime: "", endTime: "" },
              });
            } else {
              this.$message.error("删除失败!");
            }
          });
        })
        .catch(() => {});
    },

    // 批量删除
    batchDelStandard() {
      if (this.multipleSelection.length) {
        let data = [...this.multipleSelection].map((item) => {
          return "" + item.gradeStandardId;
        });

        this.$confirm("此操作将删除已选择的成绩标准, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            delStandardListApi({ ids: data.join(",") }).then((res) => {
              if (res.code === 200) {
                this.$message.success("删除成功!");
                this.getStandardList({
                  ...this.queryParams,
                  params: { beginTime: "", endTime: "" },
                });
              } else {
                this.$message.error("删除失败!");
              }
            });
          })
          .catch(() => {});
      } else {
        this.$message.error("请选择要删除的成绩标准！");
      }
    },

    // 修改启用状态
    updateIsUsing(data) {
      let { gradeStandardId, status } = data;
      editStandardListApi({
        gradeStandardId,
        status: status ? "0" : "1",
      }).then((res) => {
        if (res.code === 200) {
          this.$message.success("修改成功!");
        } else {
          this.$message.error("修改失败!");
        }
        // 获取成绩标准列表
        this.getStandardList({
          ...this.queryParams,
          params: { beginTime: "", endTime: "" },
        });
      });
    },

    // 已选中
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    // 设置当前页码
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      // 获取成绩标准列表
      this.getStandardList({
        ...this.queryParams,
        params: { beginTime: "", endTime: "" },
      });
    },

    // 跳转指定页码
    goPage() {
      let { value } = this.$refs.currentPage;
      this.queryParams.pageNum = Number(value);
      // 获取成绩标准列表
      this.getStandardList({
        ...this.queryParams,
        params: { beginTime: "", endTime: "" },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.courseBox .courseBoxTop,
.courseBox .courseBoxBottom {
  min-width: calc(130vh);
}

.courseBox .courseBoxTop .courseBoxTopSearch .pictureVersion {
  margin-left: 25px;
}

.courseBoxTopSearch {
  ::v-deep .el-input__inner {
    height: 36px;
    line-height: 36px;
  }

  .standards {
    display: flex;
    align-items: center;
    margin-bottom: 20px;

    span {
      width: 130px;
      font-size: 14px;
      text-align: right;
    }
  }
}

::v-deep .el-icon-date {
  line-height: 36px;
}

.dialog {
  .block {
    width: 280px;
    height: 30px;
    border: solid 1px #dcdfe6;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-sizing: border-box;
    background-color: #fff;

    ::v-deep .el-input__inner {
      width: 240px;
      height: 28px;
      border: none;
    }

    ::v-deep .el-icon-arrow-down {
      color: #c2c2c2;
      padding: 0 10px 0 0;
    }

    ::v-deep .el-icon-arrow-up {
      color: #c2c2c2;
      padding: 0 10px 0 0;
    }

    ::v-deep .el-date-picker.has-sidebar {
      left: 460px !important;
      background-color: #4895db;
    }
  }

  .mb,
  .px {
    ::v-deep .el-form-item__content {
      display: flex;
      width: 280px;
      line-height: 30px;
      border: solid 1px #dcdfe6;
      box-sizing: border-box;
      background-color: #fff;
      border-radius: 5px;

      .el-input__inner {
        border: none;
      }

      span {
        font-size: 14px;
        padding-left: 5px;
        color: #888;
      }
    }
  }

  ::v-deep .el-dialog {
    max-width: 750px;
    min-width: 500px;
    border-radius: 10px;
    background-color: #f9f9f9;
  }

  ::v-deep .el-dialog__header {
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    background-color: #4895db;
    padding: 0;
    border-radius: 10px 10px 0 0;

    span {
      font-size: 16px;
      color: #fff;
    }
  }

  ::v-deep .el-select > .el-input {
    display: block;
    width: 300px;
  }

  ::v-deep .el-form-item__label {
    line-height: 32px;
  }

  ::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }

  ::v-deep .el-dialog__headerbtn {
    top: 15px;
  }

  ::v-deep .el-dialog__headerbtn .el-dialog__close {
    color: #fff;
    font-size: 18px;
  }

  ::v-deep .el-dialog--center .el-dialog__body {
    text-align: initial;
    display: flex;
    justify-content: center;
  }

  ::v-deep .el-input__icon {
    line-height: 30px;
  }

  ::v-deep .el-form-item__content {
    width: 300px;
    line-height: 30px;
  }

  ::v-deep .el-form-item {
    margin-bottom: 15px;
  }

  ::v-deep .el-dialog__footer {
    padding: 10px 20px 40px;
  }

  ::v-deep .el-button--default {
    width: 130px;
    border-radius: 20px;
    background-color: #f0f0f0;
    color: #aaaaaa;
    border: none;
  }

  ::v-deep .el-button + .el-button {
    width: 130px;
    border-radius: 20px;
    margin-left: 50px;
    background-color: #4895db;
    border-color: #4895db;
  }
}
.edit{
  margin-right: 10px;
  color: #1990FF;
  cursor: pointer;
}
.del{
  margin-left: 10px;
  color: #1990FF;
  cursor: pointer;
}


.courseName,
.profession {
  ::v-deep .el-input__inner {
    width: 200px;
  }

  ::v-deep .el-date-editor.el-input {
    width: 200px;
  }
}

.pictureVersion {
  ::v-deep .el-input__inner {
    width: 140px;
  }

  ::v-deep .el-date-editor.el-input {
    width: 140px;
  }
}

.btn {
  width: 30px !important;
  height: 30px !important;
  float: right;
  margin-top: 10px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #fff !important;
  border: solid 1px #aaaaaa !important;

  .el-icon-plus {
    font-weight: bolder;
    color: #aaaaaa;
  }
}
</style>